package com.wfx.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wfx.service.IWxbOrderCountService;
import com.wfx.service.IWxbOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stat")
@CrossOrigin
@Api(description = "统计报表")
public class StatController {

    @Autowired
    private IWxbOrderService iWxbOrderService;
    @Autowired
    private IWxbOrderCountService wxbOrderCountService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper bjectMapper;
    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation("每天订单数")
    @RequestMapping("/orderCount")
    public List<Map> statOrdercuntPerDay(){



            String cuntPerMoth = (String) stringRedisTemplate.boundHashOps("stat:countOrder").get("countPerDay");
            try {
                return objectMapper.readValue(cuntPerMoth, List.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return  iWxbOrderService.statOrdercuntPerDay();

    }

    @ApiOperation("每月订单数")
    @PostMapping("/perMoth")
    public List<Map> statOrderCuntPerMoth(){
        String cuntPerMoth = (String) stringRedisTemplate.boundHashOps("stat:order").get("cuntPerMoth");
        try {
            return objectMapper.readValue(cuntPerMoth, List.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/orderCountCount")
    public List<Map> statOrdercountPerMouth(){
        String countPerMouth = (String) stringRedisTemplate.boundHashOps("stat:orderCount").get("countPerMouth");
        try {
            List<Map> list = bjectMapper.readValue(countPerMouth, List.class);
            return  list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    //每天订单的金额
    @RequestMapping("/statOderCuntPerMoney")
    public List<Map> statOderCuntPerMoney(){
        String countPerMouth = (String) stringRedisTemplate.boundHashOps("stat:countOrderMoney").get("countPerMoney");
        try {
            List<Map> list = bjectMapper.readValue(countPerMouth, List.class);
            return  list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    //统计每天的登录人数
    @RequestMapping("/loginPerson")
    public List<Map> statLoginCountPersonPerDay(){
        String cuntLoginPersonPerDay = (String) stringRedisTemplate.boundHashOps("stat:loginPerson").get("cuntLoginPersonPerDay");
        try {
            List<Map> list = objectMapper.readValue(cuntLoginPersonPerDay, List.class);
            return list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //统计每天的登录人次
    @RequestMapping("/login")
    public List<Map> statLoginCountPerDay() {
        String cuntLoginPerDay = (String) stringRedisTemplate.boundHashOps("stat:login").get("cuntLoginPerDay");
        try {
            List<Map> list = objectMapper.readValue(cuntLoginPerDay, List.class);
            return list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
